﻿/* Copyright (c) 2010, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

// Utility functions

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    class util
    {
        /** <summary>Simple implementation of Except from C# 3.0</summary>
         * <param name="first">Elements of this set are returned as a new set, unless they occur in second</param>
         * <param name="second">If an element is in this set, it prevents that value being returned should it occur in first</param>
         */

        public static IEnumerable<T> Except<T>(IEnumerable<T> first, IEnumerable<T> second)
        {
            List<T> ret = new List<T>();

            if (!(second is ICollection<T>))
                second = new List<T>(second);

            foreach (T t in first)
            {
                if (!(((ICollection<T>)second).Contains(t)))
                    ret.Add(t);
            }
            return ret;
        }

        /** <summary>Returns a dictionary containing elements within first but not second</summary> */
        public static IEnumerable<KeyValuePair<K, V>> Except<K, V>(IDictionary<K, V> first, IDictionary<K, V> second)
        {
            foreach (KeyValuePair<K, V> kvp in first)
            {
                if (!second.ContainsKey(kvp.Key))
                    yield return kvp;
            }
            yield break;
        }

        /** <summary>Simple implementation of Intersect from C# 3.0</summary>
         * <param name="first">First set</param>
         * <param name="second">Second set</param>
         */

        public static IEnumerable<T> Intersect<T>(IEnumerable<T> first, IEnumerable<T> second)
        {
            if (first == null)
                yield break;
            if (second == null)
                yield break;

            if (!(first is ICollection<T>))
                first = new List<T>(first);

            foreach (T t in second)
            {
                if (((ICollection<T>)first).Contains(t))
                    yield return t;
            }
            yield break;
        }

        /** <summary>Simple implementation of Union from C# 3.0</summary>
         * <param name="first">First set</param>
         * <param name="second">Second set</param>
         */

        public static IEnumerable<T> Union<T>(IEnumerable<T> first, IEnumerable<T> second)
        {
            if (first == null)
                yield break;
            if (second == null)
                yield break;

            if (!(first is ICollection<T>))
                first = new List<T>(first);

            foreach (T t in first)
                yield return t;

            foreach (T t in second)
            {
                if (!(((ICollection<T>)first).Contains(t)))
                    yield return t;
            }
            yield break;
        }

        /** <summary>Returns a dictionary containing those members of the initial dictionary whose keys are contained in match</summary>
         */
        public static IEnumerable<KeyValuePair<K, V>> Union<K, V>(IDictionary<K, V> initial, IEnumerable<K> match)
        {
            if (!(match is ICollection<K>))
                match = new List<K>(match);

            foreach (KeyValuePair<K, V> kvp in initial)
            {
                if (((ICollection<K>)match).Contains(kvp.Key))
                    yield return kvp;
            }
            yield break;
        }

        /** <summary>Aligns a value to a multiple of a particular value</summary>
         */
        public static int align(int input, int factor)
        {
            int i_rem_f = input % factor;
            if (i_rem_f == 0)
                return input;
            return input - i_rem_f + factor;
        }
    }
}
